package cn.uni.mapper;

import cn.uni.pojo.Emp;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface EmpMapper {

    // 根据员工ID删除信息
    @Delete("delete from emp where id = #{id}")
    public int delete(Integer id);
    // 新增一个员工
    @Options(keyProperty = "id", useGeneratedKeys = true) // 会自动将生成的主键值，赋值给emp对象的id属性
    @Insert("insert into emp(username, name,gender, image, job, entrydate, dept_id, create_time, update_time)"+
    "values(#{username}, #{name}, #{gender}, #{image}, #{job},#{entrydate}, #{deptId},#{createTime}, #{updateTime})")
    public void insert(Emp emp);

    // 根据ID更新员工信息
//    @Update("update emp set username = #{username}, name=#{name}, gender=#{gender}, image=#{image},job=#{job}, entrydate = #{entrydate}, dept_id=#{deptId}, update_time = #{updateTime} where id = #{id}")
    public void update(Emp emp);

    // 根据ID查询员工信息
    @Select("select * from emp where id = #{id}") // 存在不对应字段无法自动封装的问题
    public Emp getById(Integer id);
    // 解决方案1: 给字段起别名，起的跟实体类对应的变量名称一样
    @Select("select id, username, password, name, gender, image, job, entrydate, " +
            "dept_id deptId, create_time CreateTimee update_time updateTime from emp where id = #{id}")
    public Emp getById1(Integer id);
    // 解决方案2：通过 @Results, @Result注解手动映射封装
    @Results({
            @Result(column = "dept_id", property = "deptId"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    @Select("select * from emp where id = #{id}")
    public Emp getById2(Integer id);

    // 条件查询员工信息
//    @Select("select * from emp where name like concat('%', #{name}, '%') and gender = #{gender} and " +
//    "entrydate between #{begin} and #{end} order by update_time desc")
    public List<Emp> list(String name, Short gender, LocalDate begin, LocalDate end);


    // 批量删除员工
    public void deleteByIds(List<Integer> ids);

}
